package com.shanjupay.merchant.controller;

import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import com.shanjupay.merchant.common.util.SecurityUtil;
import com.shanjupay.transaction.api.PayChannelService;
import com.shanjupay.transaction.api.dto.PayChannelDTO;
import com.shanjupay.transaction.api.dto.PayChannelParamDTO;
import com.shanjupay.transaction.api.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(value = "商户平台-渠道和支付参数相关" ,tags = "商户平台-渠道和支付参数",description = "商户平台-渠道和支付参数")
public class PlatformParamController {


    @Reference
    private PayChannelService payChannelService ;


    @ApiOperation("获取指定应用指定服务类型下所包含的某个原始支付渠参数")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务类型" ,name = "platformChannel",required = true, dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "应用id" ,name = "payChannelParam",required = true, dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "实际支付渠道编码" ,name = "payChannel",required = true, dataType = "String",paramType = "path")
    })
    @RequestMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}",method = {RequestMethod.GET})
    public PayChannelParamDTO queryPayChannelParam(@PathVariable String appId,
                                     @PathVariable String  platformChannel,
                                     @PathVariable String  payChannel) {
        if(appId == null || platformChannel == null || payChannel == null ){
            throw new BusinessException(CommonErrorCode.E_300009);
        }

        PayChannelParamDTO paramDTO = payChannelService.queryParamByAppPlatformAndPayChannel(appId, platformChannel, payChannel);
        return paramDTO;
    }


    @ApiOperation("获取指定应用指定服务类型下所包含的原始支付渠道参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务类型" ,name = "platformChannel",required = true, dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "应用id" ,name = "payChannelParam",required = true, dataType = "String",paramType = "path")
    })
    @RequestMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}",method = {RequestMethod.GET})
    public  List<PayChannelParamDTO> queryPayChannelParam(@PathVariable String appId,@PathVariable String  platformChannel) {
        if(appId == null || platformChannel == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }

        List<PayChannelParamDTO> payChannelParamDTOS = payChannelService.queryPayChannelParamByAppAndPlatform(appId, platformChannel);
        return  payChannelParamDTOS;
    }



    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商户配置支付渠道参数" ,name = "payChannelParam",required = true, dataType = "PayChannelParamDTO",paramType = "body")
    })
    @RequestMapping(value = "/my/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    public void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParam) {
        if(payChannelParam == null || payChannelParam.getChannelName() == null){
            throw new BusinessException(CommonErrorCode.E_300009);
        }
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParam.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParam);
    }





    @ApiOperation("查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用appId",name = "appId",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(value = "服务类型" ,name = "platformChannel",required = true, dataType = "String",paramType = "query")
    })
    @GetMapping(value = "/my/merchants/apps/platformchannels")
    public int queryAppBindPlatformChannel(@RequestParam("platformChannel")String platformChannel,@RequestParam("appId")String appId) {

        return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }


    @ApiOperation("根据平台服务类型获取支付渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "服务类型编码" ,name = "platformChannelCode", required = true,dataType = "String",paramType = "path")
    })
    @GetMapping(value = "/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable("platformChannelCode")String platformChannelCode) {

        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }



    @ApiOperation("获取平台服务类型")
    @GetMapping(value = "/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel() {

        return payChannelService.queryPlatformChannel();
    }



    @ApiOperation("绑定服务类型")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用id",name = "appId",dataType = "String",paramType = "path"),
            @ApiImplicitParam(value = "服务类型code" ,name = "platformChannelCodes",required = true, dataType = "String",paramType = "query")
    })
    @PostMapping(value = "/my/apps/{appId}/platform-channels")
    public void bindPlatformForApp(@PathVariable("appId") String appId, @RequestParam("platformChannelCodes")String platformChannelCodes) {
        payChannelService.bindPlatformChannelForApp(appId,platformChannelCodes);
    }




















}
